package com.example.aop.bnapc;

import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.aop.common.ForumService;
import com.example.aop.common.ForumServiceImpl;

@Configuration
public class AppConfig {
	// 要创建代理的目标Bean
	@Bean
	public ForumService forumService() {
		return new ForumServiceImpl();
	}

	// 创建Advice或Advisor
	@Bean
	public Advice loggerMethodInterceptor() {
		return new LoggerMethodInterceptor();
	}

	// 使用BeanNameAutoProxyCreator来创建代理
	@Bean
	public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
		BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
		// 设置要创建代理的那些Bean的名字
		beanNameAutoProxyCreator.setBeanNames("forumService*");
		// 设置拦截链名字(这些拦截器是有先后顺序的)
		beanNameAutoProxyCreator.setInterceptorNames("loggerMethodInterceptor");
		return beanNameAutoProxyCreator;
	}
}